package c.e.b.m.m;

import android.content.Context;
import android.util.JsonReader;
import c.e.b.m.f.a;
import c.e.b.m.m.b.b;
import c.e.b.o.f;
import com.huawei.hms.jos.games.ranking.RankingConst;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: FirebaseInstallationServiceClient */
public class c {
    public static final Pattern d = Pattern.compile("[0-9]+s");
    public static final Charset e = Charset.forName("UTF-8");
    public final Context a;
    public final f b;
    public final c.e.b.k.c c;

    public c(Context context, f fVar, c.e.b.k.c cVar) {
        this.a = context;
        this.b = fVar;
        this.c = cVar;
    }

    /* JADX WARNING: Missing exception handler attribute for start block: B:15:0x004d */
    public static void b(java.net.HttpURLConnection r10, java.lang.String r11, java.lang.String r12, java.lang.String r13) {
        /*
        r0 = r10.getErrorStream();
        r1 = 0;
        r2 = 2;
        r3 = 1;
        r4 = 0;
        r5 = 3;
        if (r0 != 0) goto L_0x000c;
    L_0x000b:
        goto L_0x0052;
    L_0x000c:
        r6 = new java.io.BufferedReader;
        r7 = new java.io.InputStreamReader;
        r8 = e;
        r7.<init>(r0, r8);
        r6.<init>(r7);
        r0 = new java.lang.StringBuilder;	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        r0.<init>();	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
    L_0x001d:
        r7 = r6.readLine();	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        if (r7 == 0) goto L_0x002c;
    L_0x0023:
        r0.append(r7);	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        r7 = 10;
        r0.append(r7);	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        goto L_0x001d;
    L_0x002c:
        r7 = "Error when communicating with the Firebase Installations server API. HTTP response: [%d %s: %s]";
        r8 = new java.lang.Object[r5];	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        r9 = r10.getResponseCode();	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        r9 = java.lang.Integer.valueOf(r9);	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        r8[r4] = r9;	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        r10 = r10.getResponseMessage();	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        r8[r3] = r10;	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        r8[r2] = r0;	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        r10 = java.lang.String.format(r7, r8);	 Catch:{ IOException -> 0x004d, all -> 0x0048 }
        r1 = r10;
        goto L_0x004d;
    L_0x0048:
        r10 = move-exception;
        r6.close();	 Catch:{ IOException -> 0x004c }
    L_0x004c:
        throw r10;
    L_0x004d:
        r6.close();	 Catch:{ IOException -> 0x0051 }
        goto L_0x0052;
    L_0x0052:
        r10 = android.text.TextUtils.isEmpty(r1);
        if (r10 != 0) goto L_0x007d;
    L_0x0058:
        r10 = "Firebase-Installations";
        android.util.Log.w(r10, r1);
        r0 = new java.lang.Object[r5];
        r0[r4] = r12;
        r0[r3] = r13;
        r12 = android.text.TextUtils.isEmpty(r11);
        if (r12 == 0) goto L_0x006c;
    L_0x0069:
        r11 = "";
        goto L_0x0072;
    L_0x006c:
        r12 = ", ";
        r11 = c.b.a.a.a.D(r12, r11);
    L_0x0072:
        r0[r2] = r11;
        r11 = "Firebase options used while communicating with Firebase server APIs: %s, %s%s";
        r11 = java.lang.String.format(r11, r0);
        android.util.Log.w(r10, r11);
    L_0x007d:
        return;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.e.b.m.m.c.b(java.net.HttpURLConnection, java.lang.String, java.lang.String, java.lang.String):void");
    }

    public static long d(String str) {
        b.i.b.f.K(d.matcher(str).matches(), "Invalid Expiration Timestamp.");
        return (str == null || str.length() == 0) ? 0 : Long.parseLong(str.substring(0, str.length() - 1));
    }

    public static void i(URLConnection uRLConnection, byte[] bArr) throws IOException {
        OutputStream outputStream = uRLConnection.getOutputStream();
        if (outputStream != null) {
            GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(outputStream);
            try {
                gZIPOutputStream.write(bArr);
            } finally {
                try {
                    gZIPOutputStream.close();
                    outputStream.close();
                } catch (IOException unused) {
                }
            }
        } else {
            throw new IOException("Cannot send request to FIS servers. No OutputStream available.");
        }
    }

    public final URL a(String str) throws c.e.b.m.f {
        try {
            return new URL(String.format("https://%s/%s/%s", new Object[]{"firebaseinstallations.googleapis.com", "v1", str}));
        } catch (MalformedURLException e) {
            throw new c.e.b.m.f(e.getMessage(), a.UNAVAILABLE);
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:21:0x00b3 A:{Catch:{ NameNotFoundException -> 0x00db }} */
    /* JADX WARNING: Removed duplicated region for block: B:20:0x0098 A:{Catch:{ NameNotFoundException -> 0x00db }} */
    public final java.net.HttpURLConnection c(java.net.URL r9, java.lang.String r10) throws c.e.b.m.f {
        /*
        r8 = this;
        r9 = r9.openConnection();	 Catch:{ IOException -> 0x00fd }
        r9 = (java.net.HttpURLConnection) r9;	 Catch:{ IOException -> 0x00fd }
        r0 = 10000; // 0x2710 float:1.4013E-41 double:4.9407E-320;
        r9.setConnectTimeout(r0);
        r1 = 0;
        r9.setUseCaches(r1);
        r9.setReadTimeout(r0);
        r0 = "application/json";
        r2 = "Content-Type";
        r9.addRequestProperty(r2, r0);
        r2 = "Accept";
        r9.addRequestProperty(r2, r0);
        r0 = "Content-Encoding";
        r2 = "gzip";
        r9.addRequestProperty(r0, r2);
        r0 = "Cache-Control";
        r2 = "no-cache";
        r9.addRequestProperty(r0, r2);
        r0 = r8.a;
        r0 = r0.getPackageName();
        r2 = "X-Android-Package";
        r9.addRequestProperty(r2, r0);
        r0 = r8.c;
        if (r0 == 0) goto L_0x005f;
    L_0x003b:
        r2 = r8.b;
        if (r2 == 0) goto L_0x005f;
    L_0x003f:
        r2 = "fire-installations-id";
        r0 = r0.a(r2);
        r2 = c.e.b.k.c.a.NONE;
        if (r0 == r2) goto L_0x005f;
    L_0x0049:
        r2 = r8.b;
        r2 = r2.a();
        r3 = "x-firebase-client";
        r9.addRequestProperty(r3, r2);
        r0 = r0.a;
        r0 = java.lang.Integer.toString(r0);
        r2 = "x-firebase-client-log-type";
        r9.addRequestProperty(r2, r0);
    L_0x005f:
        r0 = "ContentValues";
        r2 = 0;
        r3 = r8.a;	 Catch:{ NameNotFoundException -> 0x00db }
        r4 = r3.getPackageName();	 Catch:{ NameNotFoundException -> 0x00db }
        r3 = c.e.a.b.b.k.b.a(r3);	 Catch:{ NameNotFoundException -> 0x00db }
        r3 = r3.a;	 Catch:{ NameNotFoundException -> 0x00db }
        r3 = r3.getPackageManager();	 Catch:{ NameNotFoundException -> 0x00db }
        r5 = 64;
        r3 = r3.getPackageInfo(r4, r5);	 Catch:{ NameNotFoundException -> 0x00db }
        r4 = r3.signatures;	 Catch:{ NameNotFoundException -> 0x00db }
        if (r4 == 0) goto L_0x0095;
    L_0x007c:
        r4 = r4.length;	 Catch:{ NameNotFoundException -> 0x00db }
        r5 = 1;
        if (r4 != r5) goto L_0x0095;
    L_0x0080:
        r4 = "SHA1";
        r4 = c.e.a.b.b.j.a.a(r4);	 Catch:{ NameNotFoundException -> 0x00db }
        if (r4 == 0) goto L_0x0095;
    L_0x0088:
        r3 = r3.signatures;	 Catch:{ NameNotFoundException -> 0x00db }
        r3 = r3[r1];	 Catch:{ NameNotFoundException -> 0x00db }
        r3 = r3.toByteArray();	 Catch:{ NameNotFoundException -> 0x00db }
        r3 = r4.digest(r3);	 Catch:{ NameNotFoundException -> 0x00db }
        goto L_0x0096;
    L_0x0095:
        r3 = r2;
    L_0x0096:
        if (r3 != 0) goto L_0x00b3;
    L_0x0098:
        r1 = new java.lang.StringBuilder;	 Catch:{ NameNotFoundException -> 0x00db }
        r1.<init>();	 Catch:{ NameNotFoundException -> 0x00db }
        r3 = "Could not get fingerprint hash for package: ";
        r1.append(r3);	 Catch:{ NameNotFoundException -> 0x00db }
        r3 = r8.a;	 Catch:{ NameNotFoundException -> 0x00db }
        r3 = r3.getPackageName();	 Catch:{ NameNotFoundException -> 0x00db }
        r1.append(r3);	 Catch:{ NameNotFoundException -> 0x00db }
        r1 = r1.toString();	 Catch:{ NameNotFoundException -> 0x00db }
        android.util.Log.e(r0, r1);	 Catch:{ NameNotFoundException -> 0x00db }
        goto L_0x00f2;
    L_0x00b3:
        r4 = r3.length;	 Catch:{ NameNotFoundException -> 0x00db }
        r5 = new java.lang.StringBuilder;	 Catch:{ NameNotFoundException -> 0x00db }
        r6 = r4 << 1;
        r5.<init>(r6);	 Catch:{ NameNotFoundException -> 0x00db }
    L_0x00bb:
        if (r1 >= r4) goto L_0x00d6;
    L_0x00bd:
        r6 = c.e.a.b.b.j.e.a;	 Catch:{ NameNotFoundException -> 0x00db }
        r7 = r3[r1];	 Catch:{ NameNotFoundException -> 0x00db }
        r7 = r7 & 240;
        r7 = r7 >>> 4;
        r7 = r6[r7];	 Catch:{ NameNotFoundException -> 0x00db }
        r5.append(r7);	 Catch:{ NameNotFoundException -> 0x00db }
        r7 = r3[r1];	 Catch:{ NameNotFoundException -> 0x00db }
        r7 = r7 & 15;
        r6 = r6[r7];	 Catch:{ NameNotFoundException -> 0x00db }
        r5.append(r6);	 Catch:{ NameNotFoundException -> 0x00db }
        r1 = r1 + 1;
        goto L_0x00bb;
    L_0x00d6:
        r2 = r5.toString();	 Catch:{ NameNotFoundException -> 0x00db }
        goto L_0x00f2;
    L_0x00db:
        r1 = move-exception;
        r3 = "No such package: ";
        r3 = c.b.a.a.a.g(r3);
        r4 = r8.a;
        r4 = r4.getPackageName();
        r3.append(r4);
        r3 = r3.toString();
        android.util.Log.e(r0, r3, r1);
    L_0x00f2:
        r0 = "X-Android-Cert";
        r9.addRequestProperty(r0, r2);
        r0 = "x-goog-api-key";
        r9.addRequestProperty(r0, r10);
        return r9;
    L_0x00fd:
        r9 = new c.e.b.m.f;
        r10 = c.e.b.m.f.a.UNAVAILABLE;
        r0 = "Firebase Installations Service is unavailable. Please try again later.";
        r9.m6177init(r0, r10);
        throw r9;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.e.b.m.m.c.c(java.net.URL, java.lang.String):java.net.HttpURLConnection");
    }

    public final d e(HttpURLConnection httpURLConnection) throws IOException {
        InputStream inputStream = httpURLConnection.getInputStream();
        JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream, e));
        e.a a = e.a();
        jsonReader.beginObject();
        String str = null;
        String str2 = str;
        String str3 = str2;
        e eVar = str3;
        while (jsonReader.hasNext()) {
            String nextName = jsonReader.nextName();
            if (nextName.equals(RankingConst.RANKING_JGW_NAME)) {
                str = jsonReader.nextString();
            } else if (nextName.equals("fid")) {
                str2 = jsonReader.nextString();
            } else if (nextName.equals("refreshToken")) {
                str3 = jsonReader.nextString();
            } else if (nextName.equals("authToken")) {
                jsonReader.beginObject();
                while (jsonReader.hasNext()) {
                    nextName = jsonReader.nextName();
                    if (nextName.equals("token")) {
                        ((b) a).a = jsonReader.nextString();
                    } else if (nextName.equals("expiresIn")) {
                        a.b(d(jsonReader.nextString()));
                    } else {
                        jsonReader.skipValue();
                    }
                }
                eVar = a.a();
                jsonReader.endObject();
            } else {
                jsonReader.skipValue();
            }
        }
        jsonReader.endObject();
        jsonReader.close();
        inputStream.close();
        return new a(str, str2, str3, eVar, d.a.OK, null);
    }

    public final e f(HttpURLConnection httpURLConnection) throws IOException {
        InputStream inputStream = httpURLConnection.getInputStream();
        JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream, e));
        e.a a = e.a();
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            String nextName = jsonReader.nextName();
            if (nextName.equals("token")) {
                ((b) a).a = jsonReader.nextString();
            } else if (nextName.equals("expiresIn")) {
                a.b(d(jsonReader.nextString()));
            } else {
                jsonReader.skipValue();
            }
        }
        jsonReader.endObject();
        jsonReader.close();
        inputStream.close();
        b bVar = (b) a;
        bVar.c = e.b.OK;
        return bVar.a();
    }

    public final void g(HttpURLConnection httpURLConnection, String str, String str2) throws IOException {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("fid", str);
            jSONObject.put(RankingConst.RANKING_JGW_APPID, str2);
            jSONObject.put("authVersion", "FIS_v2");
            jSONObject.put("sdkVersion", "a:16.3.3");
            i(httpURLConnection, jSONObject.toString().getBytes("UTF-8"));
        } catch (JSONException e) {
            throw new IllegalStateException(e);
        }
    }

    public final void h(HttpURLConnection httpURLConnection) throws IOException {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("sdkVersion", "a:16.3.3");
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("installation", jSONObject);
            i(httpURLConnection, jSONObject2.toString().getBytes("UTF-8"));
        } catch (JSONException e) {
            throw new IllegalStateException(e);
        }
    }
}
